package com.xiangban.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {



    /**
     * 普通的topic交换机(超时订单)
     */
    public static final String NEW_ORDER_ACCOMPANY_EXCHANGE ="new_order_accompany_exchange";

    /**
     * 路由key(超时订单)
     */
    public static final String  NEW_ORDER_ACCOMPANY_ROUTING_KEY ="new_order_accompany_routing_key";

    /**
     * 普通队列，绑定死信交换机(超时订单)
     */
    public static final String NEW_ORDER_ACCOMPANY_QUEUE ="new_order_accompany_queue";


    /**
     * 死信交换机(超时订单)
     */
    public static final String DELAY_ORDER_ACCOMPANY_EXCHANGE = "delay_order_accompany_exchange";
    /**
     * 死信队列(超时订单)
     */
    public static final String DELAY_ORDER_ACCOMPANY_QUEUE = "delay_order_accompany_queue";
    /**
     *进入死信队列的路由key(超时订单)
     */
    public static final String DELAY_ORDER_ACCOMPANY_ROUTING_KEY = "delay_order_accompany_routing_key";


    /**
     * 创建普通交换机(超时订单)
     * @return
     */
    @Bean
    public Exchange newMerchantExchange(){

        return new TopicExchange(NEW_ORDER_ACCOMPANY_EXCHANGE,true,false);
    }

    /**
     * 创建普通队列(超时订单)
     * @return
     */
    @Bean
    public Queue newMerchantQueue(){
//        HashMap<String, Object> map = new HashMap<>();
//        //消息过期后，进入到死信交换机
//        map.put("x-dead-letter-exchange",LOCK_ORDER_DEAD_EXCHANGE);
//
//        //消息过期后，进入到死信交换机的路由key
//        map.put("x-dead-letter-routing-key",LOCK_ORDER_ROUTING_KEY);
//
//        //过期时间，单位毫秒


        return  QueueBuilder.durable(NEW_ORDER_ACCOMPANY_QUEUE).withArgument("x-dead-letter-exchange",DELAY_ORDER_ACCOMPANY_EXCHANGE).
                withArgument("x-dead-letter-routing-key",DELAY_ORDER_ACCOMPANY_ROUTING_KEY).build();
    }

    /**
     * 普通交换器绑定普通队列(超时订单)
     * @return
     */
    @Bean
    public Binding newMerchantBinding(){
        return new  Binding(NEW_ORDER_ACCOMPANY_QUEUE,Binding.DestinationType.QUEUE,
                NEW_ORDER_ACCOMPANY_EXCHANGE,NEW_ORDER_ACCOMPANY_ROUTING_KEY,null);
    }

    /**
     * 创建死信交换机(超时订单)
     * @return
     */
    @Bean
    public Exchange lockMerchantDeadExchange(){
        return new TopicExchange(DELAY_ORDER_ACCOMPANY_EXCHANGE,true,false);
    }

    /**
     * 创建死信队列(超时订单)
     * @return
     */
    @Bean
    public Queue lockMerchantDeadQueue(){
        return QueueBuilder.durable(DELAY_ORDER_ACCOMPANY_QUEUE).build();
    }

    /**
     * 死信交换机绑定死信队列(超时订单)
     * @return
     */
    @Bean
    public Binding lockMerchantBinding(){
        return new Binding(DELAY_ORDER_ACCOMPANY_QUEUE,Binding.DestinationType.QUEUE,
                DELAY_ORDER_ACCOMPANY_EXCHANGE,DELAY_ORDER_ACCOMPANY_ROUTING_KEY,null);
    }

}
